﻿using System;

namespace JetBlack.TopicBus.ExcelAddin
{
    internal class TopicFieldKey : IEquatable<TopicFieldKey>, IComparable<TopicFieldKey>
    {
        internal readonly string Topic;
        internal readonly string Field;

        internal TopicFieldKey(string topic, string field)
        {
            Topic = topic;
            Field = field;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as TopicFieldKey);
        }

        public override int GetHashCode()
        {
            return
                (Topic == null ? 0 : Topic.GetHashCode()) ^
                (Field == null ? 0 : Field.GetHashCode());
        }

        public override string ToString()
        {
            return string.Concat(Topic, ":", Field);
        }

        public bool Equals(TopicFieldKey other)
        {
            return
                other != null &&
                other.Topic == Topic &&
                other.Field == Field;
        }

        public int CompareTo(TopicFieldKey other)
        {
            if (other == null)
                return 1;

            if (Topic == null && other.Topic != null)
                return -1;
            if (Field == null && other.Field != null)
                return -1;

            int i = Topic.CompareTo(other.Topic);
            if (i == 0)
                i = Field.CompareTo(other.Field);
            return i;
        }
    }
}
